*******************************************************************************
*** Table E.15: Lifetime earnings of Physicians and Lawyers

	import delimited using "${mypath}/intermediate_csv/pdv03_pdvs_physicians_pcps_lawyers.csv", clear asdoub delim(",")
	replace value = value * 1000000
	foreach x in value {
		rename `x' `x'_
	}
	
	* Add tuition
	preserve 
		import delimited "$mypath/intermediate_csv/tuit01-total_educ_cost_2017.csv", clear asdoub delim(",")
		
		loc med_tuition_range = `"\\$`=string(`=total_educ_cost_med[2]',"%15.0fc")' - \\\$`=string(`=total_educ_cost_med[1]',"%15.0fc")'"'
		loc law_tuition_range = `"\\$`=string(`=total_educ_cost_law[2]',"%15.0fc")' - \\\$`=string(`=total_educ_cost_law[1]',"%15.0fc")'"'
		
		collapse (mean) total_educ_cost_med total_educ_cost_law
		
		loc med_tuition = `=total_educ_cost_med[1]'
		loc law_tuition = `=total_educ_cost_law[1]'
	restore
	
	gen tuition_ = .
	replace tuition_ = `med_tuition' if occ == "physicians"
	replace tuition_ = `med_tuition' if occ == "pcps"
	replace tuition_ = `law_tuition' if occ == "lawyers"	
	
	gen net_ = value_ - tuition_
	sum net_ if occ == "lawyers"
	gen rel_ = (net_/`r(mean)') * 100
	reshape wide value_ tuition_ net_ rel_, i(incvar) j(occ) string 
	order incvar measure value_physicians value_pcps value_lawyers
	rename *pcps pcps_*
	rename *physicians physicians_*
	rename *lawyers lawyers_*
	foreach x in *_* {
		if regexm("`x'", "rel") tostring `x', format("%3.0fc") replace force
		else tostring `x', format("%11.0fc") replace force
	}
	reshape long physicians lawyers pcps, i(incvar) j(category) string
	tempfile pdvs
	save "`pdvs'"
	import delimited using "${mypath}/intermediate_csv/pdv04-pdv_hours.csv", clear asdoub delim(",")
	keep if bucket == "hrswork_yr"
	keep occ bucket year unscaled_hrs scaled_hrs1
	foreach occ in physicians pcps lawyers {
		preserve
			keep if occ == "`occ'"
			
			tempfile `occ'_temp
			save "``occ'_temp'"
		restore
	}
	keep if occ == "physicians"
	foreach x in unscaled_hrs scaled_hrs1 {
		rename `x' physicians_`x'
	}
	merge 1:1 bucket year using "`lawyers_temp'", nogen
	keep if bucket == "hrswork_yr"
	keep bucket year *unscaled_hrs* *scaled_hrs1*
	foreach x in unscaled_hrs scaled_hrs1 {
		rename `x' lawyers_`x'
	}
	merge 1:1 bucket year using "`pcps_temp'", nogen
	keep if bucket == "hrswork_yr"
	keep bucket year *unscaled_hrs* *scaled_hrs1*
	foreach x in unscaled_hrs scaled_hrs1 {
		rename `x' pcps_`x'
	}
	gen physicians_rel_lawyers_unscaled = (physicians_unscaled_hrs/lawyers_unscaled_hrs) * 100
	gen pcps_rel_lawyers_unscaled = (pcps_unscaled_hrs/lawyers_unscaled_hrs) * 100
	gen lawyers_rel_lawyers_unscaled = (lawyers_unscaled_hrs/lawyers_unscaled_hrs) * 100
	gen physicians_rel_lawyers_scaled = (physicians_scaled_hrs1/lawyers_scaled_hrs1) * 100
	gen pcps_rel_lawyers_scaled = (pcps_scaled_hrs1/lawyers_scaled_hrs1) * 100
	gen lawyers_rel_lawyers_scaled = (lawyers_scaled_hrs1/lawyers_scaled_hrs1) * 100
	keep year bucket *unscaled* *rel*
	foreach x in physicians_unscaled_hrs lawyers_unscaled_hrs pcps_unscaled_hrs *rel* {
		
		if regexm("`x'", "rel") {
			tostring `x', format("%3.0fc") replace force
		}
		
		else tostring `x', format("%11.0fc") replace force
	}
	reshape long physicians lawyers pcps, i(bucket) j(category) string
	append using "`pdvs'"
	* Sort results
	gen index = 1 		if category == "_value_"
	replace index = 2 	if category == "_tuition_"
	replace index = 3 	if category == "_net_"
	replace index = 4 	if category == "_rel_"
	replace index = 5 	if category == "_unscaled_hrs"
	replace index = 6 	if category == "_rel_lawyers_unscaled"
	replace index = 7 	if category == "_rel_lawyers_scaled"
	
	sort index 
	keep physicians pcps lawyers index
	order physicians pcps lawyers index
	* Table 
	texdoc init "${tables}/pdv04-table_outside_options.tex", replace
	
	texdoc stlog, nolog
	local k_value 7
		
	forvalues k = 1/`k_value' {	
		texdoc local row_`k'_col_1 = physicians[`k']		
	}
	forvalues k = 1/`k_value' {
		texdoc local row_`k'_col_2 = pcps[`k']
	}
	forvalues k = 1/`k_value' {
		texdoc local row_`k'_col_3 = lawyers[`k']
	}
	
	texdoc local med_tuition = `"\\$`=string(`med_tuition',"%15.0fc")'"'
	texdoc local law_tuition = `"\\$`=string(`law_tuition',"%15.0fc")'"'
	
	texdoc stlog close	
	
	/*tex
	\begin{table}[h!] 
	\caption{\bf Lifetime Earnings of Physicians and Lawyers \label{tab:pdv_estimates}} 
	\begin{center}
	\resizebox{1\textwidth}{!}{		
	\begin{tabular}{llccccc}  \midrule \midrule 
	& & (1) & & (2) & & (3) \\ 
	&& \shortstacking{All \\ Physicians} & & \shortstacking{Primary Care \\ Physicians}	& & Lawyers \\ \midrule
	\multicolumn{2}{l}{Mean PDV Lifetime Income} 						&\$`row_1_col_1' && \$`row_1_col_2' &&  \$`row_1_col_3' \\
	\multicolumn{2}{l}{($\beta=0.97$, at Age 20)}																									\\ [12pt]
	\multicolumn{2}{l}{Undergrad \& Graduate Tuition}					& `med_tuition' && `med_tuition' && `law_tuition' 		\\[12pt]
	\multicolumn{2}{l}{PDV Lifetime Income Net of Tuition}				&\$`row_3_col_1' && \$`row_3_col_2'	&&\$`row_3_col_3'	\\
								& ~~~~~Relative to Lawyers					&`row_4_col_1'\% && `row_4_col_2'\% && `row_4_col_3'\%	\\ [12pt]
	\multicolumn{2}{l}{Mean Lifetime Hours Worked}						&`row_5_col_1' && `row_5_col_2'	&&`row_5_col_3'			\\
								& ~~~~~Relative to Lawyers					&`row_6_col_1'\% && `row_6_col_2'\%	&& 	`row_6_col_3'\%	\\
								& ~~~~~Higher Weight for Hours $>$40 / Week &`row_7_col_1'\% && `row_7_col_2'\%	&& 	`row_7_col_3'\%	\\
	\addlinespace[1ex] \hline \hline
	\end{tabular}
	}
	\end{center}
	\end{table}
	tex*/	
	texdoc close	
